import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.*;

public class Dom {
	//doc file 
	public	Document	ReadXMLDOM(String pathFile) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = factory.newDocumentBuilder();
		File file = new File(pathFile);
		Document doc = docBuilder.parse(file);
		System.out.println("Root element : "
                + doc.getDocumentElement().getNodeName());
        if (doc.hasChildNodes()) {
            printNote(doc.getChildNodes());
        }
        return doc;

	}
	private static void printNote(NodeList nodeList) {
        for (int count = 0; count < nodeList.getLength(); count++) {
            Node tempNode = nodeList.item(count);
            // make sure it's element node.
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                // get node name and value
                System.out.println("\nNode Name = " + tempNode.getNodeName()
                        + " [OPEN]");
                System.out.println("Node Value = " + tempNode.getTextContent());

                if (tempNode.hasAttributes()) {
                    // get attributes names and values
                    NamedNodeMap nodeMap = tempNode.getAttributes();

                    for (int i = 0; i < nodeMap.getLength(); i++) {
                        Node node = nodeMap.item(i);
                        System.out.println("Attr name : " + node.getNodeName());
                        System.out.println("Attr value : "
                                + node.getNodeValue());
                    }
                }
                if (tempNode.hasChildNodes()) {
                    // loop again if has child nodes
                    printNote(tempNode.getChildNodes());
                }
                System.out.println("Node Name = " + tempNode.getNodeName()
                        + " [CLOSE]");
            }
        }
    }

	
	//tinh so nut
	public int NumBerNode	(String pathFile)	{ 
		int numberNode=0;
		try {
			Document doc = ReadXMLDOM(pathFile);
			NodeList list = doc.getElementsByTagName("*");
			numberNode= list.getLength();
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return numberNode;
	}
	
	//them 1 nut
	public	void addNode(String pathFile,String ParentNode, String NodeNeedAdd){
		try {
			Document doc = ReadXMLDOM(pathFile);
			
			Element node = doc.createElement(NodeNeedAdd);
			NodeList list = doc.getElementsByTagName("*");
			for(int i=0;i<list.getLength();i++){
				Element element = (Element) list.item(i);
				String nodeName = element.getNodeName();
				if(nodeName.equals(ParentNode)){
					element.appendChild(node);
				}
				
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//xoa 1 nut
	public	void RemoveNode(String pathFile ,String Node){
		try {
			Document doc = ReadXMLDOM(pathFile);
			
			NodeList list = doc.getElementsByTagName("*");
			for(int i=0;i<list.getLength();i++){
				Element element = (Element) list.item(i);
				String nodeName = element.getNodeName();
				if(nodeName.equals(Node)){
					element.removeChild(element);
				}
				
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
			
	//tim kiem nut cha
	public	String SearchParentNode (String pathFile, String currentNode ){
		Element node = null;
		try {
			Document doc = ReadXMLDOM(pathFile);
			
			NodeList list = doc.getElementsByTagName("*");
			for(int i=0;i<list.getLength();i++){
				Element element = (Element) list.item(i);
				String nodeName = element.getNodeName();
				if(nodeName.equals(currentNode)){
					node = (Element) element.getParentNode();
				}
				
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (node.getNodeName());
	}
	


public static void main(String args[]) throws Exception{
		Dom d = new Dom();
		d.ReadXMLDOM("C:\\Users\\TIEN\\Desktop\\PTP310034_000.xml");
		System.out.print("so nut cua xml: "+d.NumBerNode("C:\\Users\\TIEN\\Desktop\\PTP310034_000.xml"));
	}
}

